<?php
namespace Dyadmin\Controller;
use Think\Controller;
use Think\Page;
class CommomController extends Controller {
    private $roles;
	
    public function __construct(){
        parent::__construct();
        $admin_id = session('admin_id');
        $role = M('Dyadmin')->field("role_id")->where(array('admin_id'=>$admin_id))->find();
        if($role){
                $roles=	M('AdminRole')->where(array('role_id'=>$role['role_id']))->find();
        }
      $Jurisdiction_array = array();
        $roles = explode(',', $roles['grant']);
        $this->roles = $roles;
        $Jurisdiction = $this->role();
        foreach ($Jurisdiction as $key => $vol) {
            foreach ($roles as $ke => $vo) {
                if ($key == $vo && strlen($key) == strlen($vo)) {
                    $Jurisdiction_array[$key] = $vol;
                    //echo $key.'--'.$vo.'<br />';
                    $Jurisdiction_array[$key]['data'] = array();
                    foreach ($Jurisdiction[$key]['data'] as $k => $v) {
                        foreach ($roles as $rk => $rv) {
                            if ($k == $rv && strlen($k) == strlen($rv)) {
                                $Jurisdiction_array[$key]['data'][] = $v;
                            }
                        }
                    }
                }
            }
        }  
//         dump($roles);   
//         die();
        $this->assign('Jurisdiction',$Jurisdiction_array);
    }
    
    
 public function role()
    {
        return array(
            1 => array(
                'name' => '系统管理',
                'data' => array(
                	'1_2' => array('name' => '修改密码', 'url' => U('Dyadmin/system/change_pwd')),
                )
            ),
          
            3 => array(
                'name' => '会员管理',
                'data' => array(
                    '3_0' => array('name' => '我的客户', 'url' => U('Dyadmin/User/index')),
//                	'3_1' => array('name' => '个人信息图片审核', 'url' => U('Dyadmin/User/img_exa')),
//                	'3_2' => array('name' => '商户信息审核', 'url' => U('Dyadmin/User/mercha_confirm')),
                 )
            ),
         

            12 => array(
                'name' => '邀请码管理',
                'data' => array(
                    '12_0' => array('name' => '邀请码列表', 'url' => U('Dyadmin/InvitationCode/index')),
                )
            ),
        );


    }
    
    
    
    public function check($id){
        $login_session= session('login_session');
        $admin_data=D("Admin")->isLogin($login_session);
        if(IS_AJAX){
                if(empty($admin_data))die("{status:-999,url:'Dyadmin/login/index'}");
        }else{
                if(empty($admin_data))$this->redirect("Dyadmin/login/index");
        }
        
        if(IS_AJAX){
                if(!in_array($id,$this->roles))die("{status:-999,url:'Dyadmin/login/index'}");
        }else{
                if(!in_array($id,$this->roles))$this->redirect("Dyadmin/login/index");
        }
    }

    /*多表联查   zgc
   *@param  $PREFIX 表前缀
   *@param  $modelname 主表名
   * $joinarray=array("type","joinname","m_id","j_id")   $type  联查类型    left   right  $joinname  联查表名  $m_id  主表关联ID  $j_id  联表关联ID
 */
    protected function listsjoins ($PREFIX,$modelname,$where=array(),$joinarray=array(),$order='',$field=true){
        $options    =   array();
        $REQUEST    =   (array)I('request.');
        if(is_string($modelname)){
            $model  =   M($modelname);
        }
        if(is_array($joinarray) && count($joinarray)!=0){
            foreach ($joinarray as  $key=>$val){
                $model->join($val['type']." JOIN  ".$val['joinname']."  on ".$PREFIX.$modelname.".".$val['m_id']."  =  ".$val['joinname'].".".$val['j_id']);
            }
        }
        $OPT        =   new \ReflectionProperty($model,'options');
        $OPT->setAccessible(true);

        $pk         =   $model->getPk();
        if($order===null){
            //order置空
        }else if ( isset($REQUEST['_order']) && isset($REQUEST['_field']) && in_array(strtolower($REQUEST['_order']),array('desc','asc')) ) {
            $options['order'] = '`'.$REQUEST['_field'].'` '.$REQUEST['_order'];
        }elseif( $order==='' && empty($options['order']) && !empty($pk) ){
            $options['order'] = $pk.' desc';
        }elseif($order){
            $options['order'] = $order;
        }
        unset($REQUEST['_order'],$REQUEST['_field']);

        $options['where'] = array_filter(array_merge( (array)$base, /*$REQUEST,*/ (array)$where ),function($val){
            if($val===''||$val===null){
                return false;
            }else{
                return true;
            }
        });
        if( empty($options['where'])){
            unset($options['where']);
        }
        $options      =   array_merge( (array)$OPT->getValue($model), $options );
        $total        =   $model->where($options['where'])->count();

        if( isset($REQUEST['r']) ){
            $listRows = (int)$REQUEST['r'];
        }else{
            $listRows = C('LIST_ROWS') > 0 ? C('LIST_ROWS') : 10;
        }
        $page = new \Think\Page($total, $listRows, $REQUEST);
        if($total>$listRows){
            $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        }
        $p =$page->show();
        $this->assign('_page', $p? $p: '');
        $this->assign('_total',$total);
        $options['limit'] = $page->firstRow.','.$page->listRows;

        $model->setProperty('options',$options);

        return $model->field($field)->select();
    }

    protected function lists ($model,$where=array(),$order='',$base = array('status'=>array('egt',0)),$field=true){
        $options    =   array();
        $REQUEST    =   (array)I('request.');
        if(is_string($model)){
            $model  =   M($model);
        }

        $OPT        =   new \ReflectionProperty($model,'options');
        $OPT->setAccessible(true);

        $pk         =   $model->getPk();
        if($order===null){
            //order置空
        }else if ( isset($REQUEST['_order']) && isset($REQUEST['_field']) && in_array(strtolower($REQUEST['_order']),array('desc','asc')) ) {
            $options['order'] = '`'.$REQUEST['_field'].'` '.$REQUEST['_order'];
        }elseif( $order==='' && empty($options['order']) && !empty($pk) ){
            $options['order'] = $pk.' desc';
        }elseif($order){
            $options['order'] = $order;
        }
        unset($REQUEST['_order'],$REQUEST['_field']);

        $options['where'] = array_filter(array_merge( (array)$base, /*$REQUEST,*/ (array)$where ),function($val){
            if($val===''||$val===null){
                return false;
            }else{
                return true;
            }
        });
        if( empty($options['where'])){
            unset($options['where']);
        }
        $options      =   array_merge( (array)$OPT->getValue($model), $options );
        $total        =   $model->where($options['where'])->count();

        if( isset($REQUEST['r']) ){
            $listRows = (int)$REQUEST['r'];
        }else{
            $listRows = C('LIST_ROWS') > 0 ? C('LIST_ROWS') : 10;
        }
        $page = new \Think\Page($total, $listRows, $REQUEST);
        if($total>$listRows){
            $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        }
        $p =$page->show();
        $this->assign('_page', $p? $p: '');
        $this->assign('_total',$total);
        $options['limit'] = $page->firstRow.','.$page->listRows;

        $model->setProperty('options',$options);

        return $model->field($field)->select();
    }
    protected function deletewithdrawper ( $model , $where = array() , $msg = array( 'success'=>'删除成功！', 'error'=>'删除失败！')) {
        if(M($model)->where($where)->delete()!==false ) {
            $resul['info'] =$msg['success'];
            $resul["status"] = 'y';
            $this->ajaxReturn($resul, 'JSON');

        }else{
            $resul['info'] =$msg['error'];
            $resul["status"] = 'n';
            $this->ajaxReturn($resul, 'JSON');
        }
    }
}